 package handling.world;

 import client.BuddyList;
 import client.BuddylistEntry;
 import client.MapleCharacter;
 import client.inventory.Item;
 import handling.channel.ChannelServer;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import tools.MaplePacketCreator;
 import tools.packet.BuddyListPacket;
 import tools.packet.ChatPacket;










 public class WorldBuddyService
 {
/*  25 */   private static final Logger log = LogManager.getLogger(WorldBuddyService.class.getName());

   private WorldBuddyService() {
/*  28 */     log.info("正在启动[WorldBuddyService]");
   }

   public static WorldBuddyService getInstance() {
/*  32 */     return SingletonHolder.instance;
   }





   public static void buddyChat(int[] recipientCharacterIds, int cidFrom, String chattext) {
/*  40 */     buddyChat(false, recipientCharacterIds, cidFrom, chattext, null);
   }











   public static void buddyChat(boolean ItemBuddyChat, int[] recipientCharacterIds, int chrIdFrom, String chatText, Item item) {
/*  54 */     MapleCharacter chrFrom = WorldFindService.getInstance().findCharacterById(chrIdFrom);
/*  55 */     if (chrFrom == null) {
       return;
     }
/*  58 */     if (chrFrom.getChatSession() == null) {
/*  59 */       chrFrom.dropMessage(6, "聊天服务器已断开，请重新登录。");

       return;
     }
/*  63 */     chrFrom.getChatSession().writeAndFlush(ChatPacket.buddyChat(chrFrom.getAccountID(), chrIdFrom, chatText));


/*  66 */     int[] buddyids = chrFrom.getBuddylist().getBuddyIds();


/*  69 */     for (int chrid : buddyids) {
/*  70 */       int ch = WorldFindService.getInstance().findChannel(chrid);
/*  71 */       if (ch > 0) {
/*  72 */         MapleCharacter chr = ChannelServer.getInstance(ch).getPlayerStorage().getCharacterById(chrid);
/*  73 */         if (chr != null && chr.getBuddylist().containsVisible(chrIdFrom)) {

/*  75 */           if (!ItemBuddyChat)
           {

/*  78 */             chr.getChatSession().writeAndFlush(ChatPacket.buddyChat(chrFrom.getAccountID(), chrIdFrom, chatText));
           }

/*  81 */           if (chr.getClient().isMonitored()) {
/*  82 */             WorldBroadcastService.getInstance().broadcastGMMessage(MaplePacketCreator.serverNotice(6, "[GM消息] " + chrFrom.getName() + " said to " + chr.getName() + " (好友): " + chatText));
           }
         }
       }
     }
   }

   private void updateBuddies(int characterId, int channel, int[] buddies, boolean offline) {
/*  90 */     for (int buddy : buddies) {
/*  91 */       int ch = WorldFindService.getInstance().findChannel(buddy);
/*  92 */       if (ch > 0) {
/*  93 */         MapleCharacter chr = ChannelServer.getInstance(ch).getPlayerStorage().getCharacterById(buddy);
/*  94 */         if (chr != null) {
/*  95 */           BuddylistEntry ble = chr.getBuddylist().get(characterId);
/*  96 */           if (ble != null && ble.isVisible()) {
             int mcChannel;
/*  98 */             if (offline) {
/*  99 */               ble.setChannel(-1);
/* 100 */               mcChannel = -1;
             } else {
/* 102 */               ble.setChannel(channel);
/* 103 */               mcChannel = channel - 1;
             }
/* 105 */             chr.send(BuddyListPacket.updateBuddyChannel(ble.getCharacterId(), mcChannel, ble.getName()));
           }
         }
       }
     }
   }

   public void buddyChanged(int chrId, int chrIdFrom, String name, int channel, BuddyList.BuddyOperation operation, String group) {
/* 113 */     int ch = WorldFindService.getInstance().findChannel(chrId);
/* 114 */     if (ch > 0) {
/* 115 */       MapleCharacter addChar = ChannelServer.getInstance(ch).getPlayerStorage().getCharacterById(chrId);
/* 116 */       if (addChar != null) {
/* 117 */         BuddyList buddylist = addChar.getBuddylist();
/* 118 */         switch (operation) {
           case 添加好友:
/* 120 */             if (buddylist.contains(chrIdFrom)) {
/* 121 */               buddylist.put(new BuddylistEntry(name, chrIdFrom, group, channel, true));
/* 122 */               addChar.getClient().announce(BuddyListPacket.updateBuddylist(buddylist.getBuddies(), 23));
             }
             break;
           case 删除好友:
/* 126 */             if (buddylist.contains(chrIdFrom)) {
/* 127 */               buddylist.remove(chrIdFrom);
/* 128 */               addChar.getClient().announce(BuddyListPacket.updateBuddylist(buddylist.getBuddies(), 23));
             }
             break;
         }
       }
     }
   }

   public BuddyList.BuddyAddResult requestBuddyAdd(String addName, int channelFrom, int chrIdFrom, String nameFrom, int levelFrom, int jobFrom) {
/* 137 */     int ch = WorldFindService.getInstance().findChannel(chrIdFrom);
/* 138 */     if (ch > 0) {
/* 139 */       MapleCharacter addChar = ChannelServer.getInstance(ch).getPlayerStorage().getCharacterByName(addName);
/* 140 */       if (addChar != null) {
/* 141 */         BuddyList buddylist = addChar.getBuddylist();
/* 142 */         if (buddylist.isFull()) {
/* 143 */           return BuddyList.BuddyAddResult.好友列表已满;
         }
/* 145 */         if (!buddylist.contains(chrIdFrom)) {
/* 146 */           buddylist.addBuddyRequest(addChar.getClient(), chrIdFrom, nameFrom, channelFrom, levelFrom, jobFrom);
         }
/* 148 */         else if (buddylist.containsVisible(chrIdFrom)) {
/* 149 */           return BuddyList.BuddyAddResult.已经是好友关系;
         }
       }
     }

/* 154 */     return BuddyList.BuddyAddResult.添加好友成功;
   }

   public void loggedOn(String name, int chrId, int channel, int[] buddies) {
/* 158 */     updateBuddies(chrId, channel, buddies, false);
   }

   public void loggedOff(String name, int chrId, int channel, int[] buddies) {
/* 162 */     updateBuddies(chrId, channel, buddies, true);
   }

   private static class SingletonHolder
   {
/* 167 */     protected static final WorldBuddyService instance = new WorldBuddyService();
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\world\WorldBuddyService.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */